﻿using DanaZhangCms.Domain.SiteXml;
using DanaZhangCms.Domain.ViewModel;
using DanaZhangCms.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DanaZhangCms.Controllers.Base
{
    public class BaseController : Controller
    {
        //工作上下文
        public WebWorkContext WorkContext = new WebWorkContext();



        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var filePath = System.IO.Path.Combine(GlobalConfiguration.ApplicationPath, "XmlConfig", "site.config");
            var site = DanaZhangCms.Helper.XmlHelper.XmlDeserializeFromFile<Site>(filePath, System.Text.Encoding.UTF8);
            WorkContext.Sites = site;
        }

        public override void OnActionExecuted(ActionExecutedContext context)
        {
            ViewBag.WorkContext = WorkContext;
            ViewBag.Title = WorkContext.Sites.webname;
        }



        /// <summary>
        /// 获得路由中的值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        protected string GetRouteString(string key, string defaultValue)
        {
            object value = RouteData.Values[key];
            if (value != null)
                return value.ToString();
            else
                return defaultValue;
        }

        /// <summary>
        /// 获得路由中的值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        protected string GetRouteString(string key)
        {
            return GetRouteString(key, "");
        }


    }
}
